package com.runfast.paotui.service.impl;

import com.runfast.common.exception.BaseException;
import com.runfast.common.service.BaseService;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.paotui.dao.mapper.RunfastCuserMapper;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.dao.model.RunfastCuserExample;
import com.runfast.paotui.dao.model.RunfastCuseraddress;
import com.runfast.paotui.service.RunfastCuserService;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RunfastCuserServiceImpl extends BaseService<RunfastCuser, Integer, RunfastCuserExample> implements RunfastCuserService {
  @Resource
    RunfastCuserMapper dao;
    @Override
    public RunfastCuser getUserBy(String name, String password) {
        Validate.notBlank(name);
        Validate.notBlank(password);


        RunfastCuserExample runfastCuserExample = new RunfastCuserExample();
        runfastCuserExample.createCriteria().andMobileEqualTo(name);
        List<RunfastCuser> runfastCusers = this.selectByExample(runfastCuserExample);

        if (runfastCusers.size() == 0) throw new BaseException(ResultCode.ACCOUNT_NOT_EXIST);


        RunfastCuser runfastCuser = runfastCusers.get(0);

        if (!runfastCuser.getPassword().equalsIgnoreCase(password))
            throw new BaseException(ResultCode.ACCOUNT_USERNAME_OR_PASSWORD_ERROR);

        return runfastCuser;
    }

    @Override
    public RunfastCuser get(Integer id) {
        return dao.get(id);
    }

    @Override
    public RunfastCuseraddress findToid(Integer toId) {
        return dao.findToid(toId);
    }
}